<template>
  <basic-container>
    <div class="container-body">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="uid">
          <el-input size="small" v-model="query.userId" placeholder="uid"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input size="small" v-model="query.realname" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input size="small" v-model="query.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-form>
      <el-table header-row-class-name="container-table-header"
                :data="data" size="small"
                style="width: 100%;margin-bottom: 20px;"
                row-key="userId"
                border stripe
                v-loading="loading">
        <el-table-column prop="avatar" label="头像" width="100">
          <template slot-scope="scope">
            <el-image :src="scope.row.avatar">
              <div slot="error" class="image-slot">
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="uid">
        </el-table-column>
        <el-table-column prop="nickName" label="昵称">
        </el-table-column>
        <el-table-column prop="realname" label="姓名">
        </el-table-column>
        <el-table-column prop="phone" label="手机号">
        </el-table-column>
<!--        <el-table-column prop="userLevel" label="等级">
        </el-table-column>
        <el-table-column prop="userIntegral" label="积分">
        </el-table-column>
        <el-table-column prop="userMedal" label="勋章">
        </el-table-column>-->
        <el-table-column prop="createTime" label="注册时间">
        </el-table-column>
        <el-table-column prop="userType" label="用户类型">
          <template slot-scope="scope">
            {{ scope.row.userType | userType }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作" width="170">
          <template slot-scope="scope">
            <el-button ype="text" size="small" icon="el-icon-edit" v-if="permissions.exam_customer_add" @click="showEditBox(scope.row)">编辑</el-button>
<!--            <el-button ype="text" size="small" icon="el-icon-view" @click="showQrCode(scope.row)">小程序推广二维码</el-button>
            <el-button ype="text" size="small" icon="el-icon-view" @click="showInviteRecord(scope.row)">邀请记录</el-button>-->
          </template>
        </el-table-column>-
      </el-table>
      <div class="container-page">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>

    <el-dialog title="编辑" :visible.sync="formBox" :close-on-click-modal="!formBox" :append-to-body="true" :before-close="resetForm">
      <el-form ref="optForm" :model="form" label-width="100px">
        <el-form-item label="uuid" prop="userId">
          <el-input v-model="form.userId" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone" :rules="{required: true, message: '手机号不能为空', trigger: ['blur']}">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="userType" :rules="{required: true, message: '用户类型不能为空', trigger: ['blur']}">
          <el-select size="small" v-model="form.userType" placeholder="请选择" clearable>
            <el-option label="客户" :value="0"></el-option>
            <el-option label="景区员工" :value="1"></el-option>
            <el-option label="商家员工" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商家" prop="shopName" v-if="form.userType === 2" :rules="{required: true, message: '商家不能为空', trigger:'blur'}">
          <el-row>
            <el-col :span="14"><el-input size="small" v-model="form.shopName" placeholder="商家" disabled></el-input></el-col>
            <el-col :span="10"><el-button @click="showShopDialog()">请选择</el-button></el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button type="success" @click="submit" size="small" icon="el-icon-check" :loading="loading">保存</el-button>
      </span>
    </el-dialog>

    <el-dialog title="邀请记录" :visible.sync="inviteRecordBox" :close-on-click-modal="!inviteRecordBox"
               :append-to-body="true" :before-close="resetForm" width="1000px" top="5vh">
      <el-table header-row-class-name="container-table-header"
                :data="inviteRecordList" size="small"
                style="width: 100%;margin-bottom: 20px;"
                row-key="refundId"
                border stripe
                v-loading="loading">
        <el-table-column type="index" width="50">
        </el-table-column>
        <el-table-column prop="invitedUserId" label="被邀请人uid">
        </el-table-column>
        <el-table-column prop="invitedUserPhone" label="被邀请人手机号">
        </el-table-column>
        <el-table-column prop="createTime" label="添加时间">
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
                <el-button @click="resetForm" size="small" icon="el-icon-close">取消</el-button>
            </span>
    </el-dialog>

  </basic-container>
</template>

<script>
import {fetchList, inviteRecordList, submit, getObj} from "@/api/user/customer";
import {mapGetters} from "vuex";

export default {
  components: {
  },
  data() {
    return {
      importBox: false,
      isEdit: false,
      isView: false,
      viewBox: false,
      formBox: false,
      formTitle: '',
      loading: true,
      query: {},
      form: {},
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      showUploadButton: true,
      fileList: [],
      qrcodeBox: false,
      inviteRecordBox: false,
      inviteRecordList: [],
      shopDialog: false,
    }
  },
  computed: {
    ...mapGetters(["userInfo", "permissions"])
  },
  created() {
    this.onLoad(this.page);
  },
  methods: {
    // 获取关联题库
    setRelateBook(val) {
      if (val) {
        this.query.bookId = val.bookId;
        this.query.bookName = val.name;
      }
    },
    // 显示题库列表
    showBookDialog() {
      this.bookDialog = true
    },
    // 关闭
    closeBookDialog(val) {
      this.bookDialog = val
    },
    resetForm() {
      this.formBox = false;
      this.isEdit = false;
      this.isView = false;
      this.form = {};
      this.formTitle = '';
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.viewBox = false;
      this.fileList = [];
      this.qrcodeBox = false;
      this.inviteRecordBox = false;
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.current = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      });
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      this.showUploadButton = true
    },
    handleSuccess(response, file, fileList) {
      this.fileList = fileList;
      if (this.fileList.length >= 1) {
        this.showUploadButton = false
      }
    },
    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    showQrCode(row) {
      getAcodeUnlimit({userId: row.userId}).then((res) => {
        this.qrcodeBox = true;
        this.form.inviteCodeUrl = res.data.data;
      });
    },
    showInviteRecord(row) {
      inviteRecordList({inviteUserId: row.userId}).then((res) => {
        this.inviteRecordBox = true;
        this.inviteRecordList = res.data.data;
      });
    },
    showEditBox(row) {
      getObj(row.userId).then((res) => {
        this.form = res.data.data;
        this.formTitle = '编辑';
        this.formBox = true;
        this.isEdit = true;
      });
    },
    submit() {
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          submit(this.form).then(() => {
            this.$message({
              type:"success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },

    // 获取关联店铺
    setRelateShop(val) {
      if (val) {
        this.form.shopId = val.shopId;
        // this.form.shopName = val.shopName;
        this.$set(this.form, 'shopName', val.shopName)
      }
    },
    // 显示店铺列表
    showShopDialog() {
      this.shopDialog = true
    },
    // 关闭店铺
    closeShopDialog(val) {
      this.shopDialog = val
    },
  }
};
</script>

<style>
</style>
